package cfca.seal.web.tag;

import cfca.seal.bean.Organization;
import cfca.seal.dao.main.IOrganizationDao;
import cfca.seal.web.tag.base.TemplateDirectiveModelBase;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class OrganizationNameTag extends TemplateDirectiveModelBase
{

  @Resource
  IOrganizationDao dao;

  public String process(Environment env, Map paramsMap, TemplateModel[] loopVars, TemplateDirectiveBody body)
    throws TemplateException, IOException
  {
    StringBuffer sf = new StringBuffer();
    if ((this.elementValue != null) && (!this.elementValue.isEmpty())) {
      try {
        Organization org = this.dao.getOrganizationById(this.elementValue);
        if (org != null)
          sf.append("" + org.getName());
        else
          sf.append("" + this.elementValue);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    sf.append("");
    return sf.toString();
  }
}